﻿using System;
using System.Collections.Generic;
using System.Linq;
using DataAccess.Repository.Interface;
using System.Data;
using System.Data.SqlClient;

namespace DataAccess.Repository
{
    public class DynamicWorkRepository : IDynamicWorkRepository
    {
        private KPIDbContext _KPIDBContext = new KPIDbContext();

        #region 新增工作动态
        /// <summary>
        ///新增工作动态
        /// </summary>
        /// <param name="wn"></param>
        /// <returns></returns>
        public bool CreateWorkNews(Models.WorkNews wn)
        {
            var result = false;
            try
            {
                _KPIDBContext.WorkNews.Add(wn);
                _KPIDBContext.SaveChanges();
                result = true;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
        #endregion

        #region 编辑工作动态
        /// <summary>
        /// 编辑工作动态
        /// </summary>
        /// <param name="wn"></param>
        /// <returns></returns>
        public bool EditWorkNews(Models.WorkNews wn)
        {
            var result = false;

            if (wn != null && wn.WorkNewsID > 0)
            {
                try
                {
                    _KPIDBContext.Entry(wn).State = EntityState.Modified;
                    _KPIDBContext.SaveChanges();

                    result = true;
                }
                catch
                {
                    throw;
                }
            }
            return result;
        }
        #endregion

        #region 获取工作动态分页
        /// <summary>
        /// 获取工作动态分页
        /// </summary>
        /// <param name="keys">名称</param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageCount"></param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public IList<Models.WorkNews> GetWorkNewsListPageing(string keys, int PageSize, int PageIndex, out int PageCount, out int rowCount)
        {
            IList<Models.WorkNews> List = new List<Models.WorkNews>();

            SqlParameter p1 = new SqlParameter("@PageCount", null);
            p1.DbType = DbType.Int32;
            p1.Direction = ParameterDirection.Output;

            SqlParameter p2 = new SqlParameter("@rowCount", null);
            p2.DbType = DbType.Int32;
            p2.Direction = ParameterDirection.Output;

            SqlParameter[] sp = new SqlParameter[]{
               new SqlParameter("@pagesize",PageSize),
               new SqlParameter("@pageindex",PageIndex),
               p1,p2,
                new SqlParameter("@keys",keys)
           };

            DataTable dtRes = SqlHelper.GetDataSet(CommandType.StoredProcedure, "proc_selectWorkNewsPageing", sp);
            PageCount = Convert.ToInt32(p1.Value);
            rowCount = Convert.ToInt32(p2.Value);

            foreach (DataRow reader in dtRes.Rows)
            {
                Models.WorkNews wn = new Models.WorkNews();
                wn.WorkNewsID = Convert.ToInt32(reader["WorkNewsID"]);
                wn.WorkNewsTitle = reader["WorkNewsTitle"].ToString();
                wn.WorkNewsContent = reader["WorkNewsContent"].ToString();
                wn.WorkNewsAddTime = Convert.ToDateTime(reader["WorkNewsAddTime"].ToString());
                wn.WorkNewsModifyTime = Convert.ToDateTime(reader["WorkNewsModifyTime"].ToString());
                List.Add(wn);
            }
            return List;
        }
        #endregion

        #region 获取工作动态信息内容
        /// <summary>
        /// 获取工作动态信息内容
        /// </summary>
        /// <param name="wnid"></param>
        /// <returns></returns>
        public Models.WorkNews GetDetailWorkNews(int wnid)
        {
            return _KPIDBContext.WorkNews.FirstOrDefault(w => w.WorkNewsID == wnid);
        }
        #endregion

        #region 删除工作动态
        /// <summary>
        /// 删除工作动态
        /// </summary>
        /// <param name="wnIDs"></param>
        /// <returns></returns>
        public bool DeleteWorkNews(List<int> wnIDs)
        {
            var result = false;

            if (wnIDs != null && wnIDs.Count > 0)
            {
                try
                {
                    foreach (var id in wnIDs)
                    {
                        var wn = _KPIDBContext.WorkNews.Find(id);

                        if (wn != null)
                        {
                            _KPIDBContext.WorkNews.Remove(wn);
                        }
                    }

                    if (_KPIDBContext.SaveChanges() > 0)
                    {
                        result = true;
                    }
                }
                catch
                {
                    throw;
                }
            }
            return result;
        }
        #endregion
       

    }
}
